<?php
namespace app\api\controller;
use think\Controller;
use think\Db;
use lib\UserCrypt;
use xiaoniuyun\SendSms;
class Login extends Base{
	public function sendSms()
	{
		if(!empty(input("param.phone"))){
			$phone=input("param.phone");
		}else{
			return json(['code' => 501, 'msg' => '请填写手机号']);
		}
		$verifyCode = rand(1000,9999);
		$result = Db::name('verify_code')->insertGetId(['phone' => $phone, 'code' => $verifyCode,'time' => time()]);
		if(!$result){
			return json(['code' => 502, 'msg' => '发送失败，稍后重试哟']);
		}else{
			$sendSms = new SendSms();
			$verifyCodearr=array("code" => $verifyCode);
			$result_content = $sendSms->sendSms($phone,$verifyCodearr,503);
			if($result_content->Message=='OK'){
				return json(['code' => 200, 'msg' => '发送成功','data'=>$verifyCode]);
			}else{
				return json(['code' => 503, 'msg' => '发送失败,错误码：'.$result_content->result.',错误说明：'.$result_content->Message]);
			}
		}
	}
	public function register(){
		if(!empty(input("param.phone"))){
			$data['phone']=input("param.phone");
		}else{
			return json(['code' => 501, 'msg' => '请填写手机号']);
		}
		if(!empty(input("param.invite_code"))){
			$data['invite_code']=input("param.invite_code");
		}else{
			return json(['code' => 502, 'msg' => '请填写邀请码']);
		}
		if(empty(input("param.verify_code"))){
			return json(['code' => 503, 'msg' => '请填写验证码']);
		}
        $user=Db::name('users')->where(array('phone'=>$data['phone']))->find();
		if (empty($user)) {
			$invite_code=Db::name('users')->where(array('invite_code'=>$data['invite_code']))->find();
			if(!empty($invite_code)){
				$verify_code=Db::name('verify_code')->where(array('phone'=>$data['phone'],'code'=>input("param.verify_code")))->order('id desc')->find();
				if(!empty($verify_code)){
					$data['invite_id']=$invite_code['id'];
					$data['time']=time();
					$data['uptime']=time();
					$id=Db::name('users')->insertGetId($data);
					if($id){
						$res['Token']=UserCrypt::do_mencrypt($id);
						return json(['code' => 200, 'msg' => '注册成功','data'=>$res]);
					}else{
						return json(['code' => 500, 'msg' => '注册失败']);
					}
				}else{
					return json(['code' => 506, 'msg' => '验证码错误']);
				}
			}else{
				return json(['code' => 505, 'msg' => '邀请码错误']);
			}
		}else{
			return json(['code' => 504, 'msg' => '该手机号注册过了']);
		}
	}
  	//登录
	public function login(){
		if(!empty(input("param.phone"))){
			$data['phone']=input("param.phone");
		}else{
			return json(['code' => 501, 'msg' => '手机号不能为空']);
		}
		// 1：密码登录;2：验证码登录
		if(input("param.login_type")==1){
			if(!empty(input("param.password"))){
				$data['password']=input("param.password");
			}else{
				return json(['code' => 503, 'msg' => '密码不能为空']);exit();
			}
			$user=Db::name('users')->where(array('phone'=>$data['phone']))->find();
			if ($user) {
				if($user['password']==md5(md5('tmd'.$data['password']))){
					$res['Token']=UserCrypt::do_mencrypt($user['id']);
					return json(['code' => 200, 'msg' => '登录成功','data'=>$res]);
				}else{
					return json(['code' => 505, 'msg' => '密码错误']);
				}
			}else{
				return json(['code' => 504, 'msg' => '手机号不存在']);
			}
		}elseif(input("param.login_type")==2){
			if(!empty(input("param.verify_code"))){
				$data['verify_code']=input("param.verify_code");
			}else{
				return json(['code' => 506, 'msg' => '验证码不能为空']);exit();
			}
			$user=Db::name('users')->where(array('phone'=>$data['phone']))->find();
			if ($user) {
				$verify_code=Db::name('verify_code')->where(array('phone'=>$data['phone'],'code'=>$data['verify_code']))->order('id desc')->find();
				if ($verify_code) {
					$res['Token']=UserCrypt::do_mencrypt($user['id']);
					return json(['code' => 200, 'msg' => '登录成功','data'=>$res]);
				}else{
					return json(['code' => 507, 'msg' => '验证码错误']);
				}
			}else{
				return json(['code' => 504, 'msg' => '手机号不存在']);
			}
			
		}else{
			return json(['code' => 502, 'msg' => '验证码登录还是密码登录？']);
		}
		
	}
	//更改密码
	public function changePassword(){
		if(!empty(input("param.token"))){
            $token=input("param.token");
        }else{
            return json(['code' => 501, 'msg' => '令牌呢？']);exit();
        }
		if(!empty(input("param.verify_code"))){
			$data['verify_code']=input("param.verify_code");
		}else{
			return json(['code' => 502, 'msg' => '验证码不能为空']);exit();
		}
		if(!empty(input("param.password"))){
			$data['password']=input("param.password");
		}else{
			return json(['code' => 503, 'msg' => '密码不能为空']);exit();
		}
        $id=UserCrypt::do_mdecrypt($token);
        $user=Db::name('users')->where('id', $id)->find();
		if ($user) {
			$verify_code=Db::name('verify_code')->where(array('phone'=>$user['phone'],'code'=>$data['verify_code']))->order('id desc')->find();
			if ($verify_code) {
				$result=Db::name('users')->where('id', $id)->update(array('password'=>md5(md5('tmd'.$data['password'])),'uptime'=>time()));
				if($result){
					return json(['code' => 200, 'msg' => '更改成功']);exit();
				}else{
					return json(['code' => 506, 'msg' => '更新失败']);exit();
				}
			}else{
				return json(['code' => 505, 'msg' => '验证码错误']);
			}
		}else{
			return json(['code' => 504, 'msg' => '令牌错误']);
		}
	}
}